package com.wdhcr.service.imp;

import com.wdhcr.entity.Link;
import com.wdhcr.repository.LinkRepository;
import com.wdhcr.service.LinkService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import java.util.List;
@Service
public class LinkServiceImp implements LinkService {

    @Autowired
    private LinkRepository linkRepository;
    @Override
    public Link saveLink(Link link) {
        return linkRepository.save(link);
    }

    @Override
    public Link getLink(Long id) {
        return linkRepository.findById(id).get();
    }

    @Override
    public List<Link> listLink() {
        return null;
    }

    @Override
    public Link getLinkByName(String name) {
        return null;
    }

    @Override
    public List<Link> listLinkTop(Integer size) {
        //根据更新时间倒叙排列
        Sort sort = Sort.by(Sort.Direction.DESC,"weight");
        PageRequest pageRequest = PageRequest.of(0, size,sort);
        return linkRepository.findAll(pageRequest).toList();
    }

    @Override
    public Page<Link> listLink(Pageable pageable) {
        return linkRepository.findAll(pageable);
    }


    @Override
    public void deleteById(Long id) {
        linkRepository.deleteById(id);
    }
}
